<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let arr = [50, 40, 20, 10, 30]
    console.log(arr)

    //冒泡从大到小
    for (let i = 0; i < arr.length - 1; i++) {
      for (let j = 0; j < arr.length - i - 1; j++) {
        if (arr[j] < arr[j + 1]) {
          let num = arr[j]
          arr[j] = arr[j + 1]
          arr[j + 1] = num
        }
      }
    }
    console.log(arr)

    //冒泡从小到大
    for (let i = 0; i < arr.length - 1; i++) {
      for (let j = 0; j < arr.length - i - 1; j++) {
        if (arr[j] > arr[j + 1]) {
          let num = arr[j]
          arr[j] = arr[j + 1]
          arr[j + 1] = num
        }
      }
    }
    console.log('小到大数组' + arr)
    console.log('-------------------------------------------------------------');
    let a = [4, 14, 3, 11, 7, 9, 1, 12, 10, 6, 15, 2, 8, 13, 5]
    console.log(a)
    // a.sort()  sort（按字符串）自动排序 默认升序排序
    //默认按字符串来升序，先比较第一个字符，再第二个...一直比下去
    a.sort()
    console.log(a)
    // 1.升序
    a.sort(function (a, b) {
      return a - b
    })
    console.log(a)
    // 2.降序
    a.sort(function (a, b) {
      return b - a
    })
    console.log(a)
  </script>
</body>

</html>